home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  3.3 KB  |  197 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28.  
  29. /*
  30.  *  Routines to save and load missions
  31.  */
  32.  
  33. void ReadSaves()
  34. /*
  35.  *  Write the save file
  36.  */
  37. {
  38.   FILE *fd;
  39.  
  40.   nsaves = 0;
  41.  
  42.   /* Open the save file */
  43.   if (NULL == (fd = fopen ("saves.txt", "rt")))
  44.   {
  45.     /* No saves */
  46.     return;
  47.   }
  48.  
  49.   /* Read through the file */
  50.   while (2 == (fscanf (fd, "%s %d", save[nsaves].fn,
  51.   &save[nsaves].time)))
  52.   {
  53.     nsaves++;
  54.  
  55.     /* Too many? */
  56.     if (nsaves >= NSAVES)
  57.     {
  58.       nsaves = NSAVES;
  59.       fclose (fd);
  60.       return;
  61.     }
  62.   }
  63.  
  64.   /* That's it! */
  65.   fclose (fd);
  66.   return;
  67. }
  68.  
  69. void WriteSaves()
  70. /*
  71.  *  Write the save file
  72.  */
  73. {
  74.   FILE *fd;
  75.   int s;
  76.  
  77.   /* Open the file */
  78.   if (NULL == (fd = fopen ("saves.txt", "wt")))
  79.   {
  80.     Mprint ("Can't open save file!");
  81.     return;
  82.   }
  83.  
  84.   for (s=0; s<nsaves; s++)
  85.   {
  86.     fprintf (fd, "%s %d\n", save[s].fn, save[s].time);
  87.   }
  88.  
  89.   /* All done */
  90.   fclose (fd);
  91.  
  92.   return;
  93. }
  94.  
  95. void AddSave (char *fn)
  96. /*
  97.  *  Add a mission to the list of saves
  98.  */
  99. {
  100.   int s, sm;
  101.  
  102.   /* Read the file */
  103.   ReadSaves();
  104.  
  105.   /* See if we can find one with same mission name */
  106.   sm = -1;
  107.   for (s=0; s<nsaves; s++)
  108.   {
  109.     if (!strcmp (fn, save[s].fn))
  110.     {
  111.       /* Found one */
  112.       sm = s;
  113.       break;
  114.     }
  115.   }
  116.  
  117.   /* Didn't find one */
  118.   if (sm < 0)
  119.   {
  120.     if (nsaves < NSAVES) nsaves++;
  121.     sm = nsaves - 1;
  122.   }
  123.  
  124.   /* Make room */
  125.   for (s=sm; s>0; s--)
  126.   {
  127.     strcpy (save[s].fn, save[s-1].fn);
  128.     save[s].time = save[s-1].time;
  129.   }
  130.  
  131.   /* Plop this one in */
  132.   strcpy (save[0].fn, fn);
  133.   save[0].time = time(NULL);
  134.  
  135.   /* Write out the file */
  136.   WriteSaves();
  137. }
  138.  
  139. void LoadGame()
  140. /*
  141.  *  Sparky wants to load a game
  142.  */
  143. {
  144.   int s;
  145.  
  146.   char buf[4096], buf2[32];
  147.  
  148.   /* Read the saves file */
  149.   ReadSaves();
  150.  
  151.   /* Don't bother if no saves */
  152.   if (nsaves == 0)
  153.   {
  154.     Mprint ("No saved games!");
  155.     return;
  156.   }
  157.  
  158.   /* Pause the game */
  159.   paused = 1;
  160.  
  161.   /* Set the game state */
  162.   state = STATE_LOADGAME;
  163.  
  164.   /* Construct the selection list */
  165.   strcpy (buf, "Select mission to load:\\\\");
  166.  
  167.   for (s=0; s<nsaves; s++)
  168.   {
  169.     sprintf (buf2, "%d) ", s);
  170.     strcat (buf, buf2);
  171.     strcat (buf, save[s].fn);
  172.     strcat (buf, " ");
  173.     sprintf (buf2, "%s", ctime((const time_t *)&save[s].time));
  174.     strcat (buf, buf2);
  175.     strcat (buf, "\\");
  176.   }
  177.  
  178.   Mprint (buf);
  179. }
  180.  
  181. void LoadGameByKey (int k)
  182. /*
  183.  *  Load game depending on the key Sparky pressed
  184.  */
  185. {
  186.   if ( (k < 0) || (k >= nsaves) ) return;
  187.  
  188.   /* Set state back to normal */
  189.   state = STATE_NORMAL;
  190.  
  191.   /* Read the mission */
  192.   ReadMission (save[k].fn);
  193.  
  194.   /* That's it! */
  195.   return;
  196. }
  197.